#ifndef MQTT_H
#define MQTT_H

#include "./delay/delay.h"
#include "./usart/usart.h"
#include <stdlib.h>
#include <string.h>

#define PROD_ID "276125"            // 产品ID
#define SN "CSICLD"                 // 标记名称也是鉴权
#define REG_CODE "hjjyhyL8EhZWzg7O" // 注册码
#define API_ADDR "api.heclouds.com" // 登录的API
#define DEVICE_NAME "LD123456"      // 设备名称
#define REG_PKT_HEAD "POST http://" API_ADDR "/register_de?register_code=" REG_CODE " HTTP/1.1\r\n" \
                     "Host: " API_ADDR "\r\n"                                                       \
                     "Content-Length: " // http post数据头

#define REG_PKT_BODY "{\"title\":\"" DEVICE_NAME "\",\"sn\":\"" SN "\"}" // 数据内容

struct MqttContext
{
    u8 device_id[20];
};
char *uartDataParse(char *buffer, int32_t *plen);

int MqttSample_RecvPkt(void);
u8 mqtt_ping_message(u8 *mqtt_message);
u8 mqtt_disconnect_message(u8 *mqtt_message);
u8 mqtt_disconnect_message(u8 *mqtt_message);
u8 MqttOnenet_Savedata(u8 *t_payload, float lon, float lat);
u8 MqttOnenet_SavedataTEMP(u8 *t_payload, u8 temp, u8 humi);
u16 mqtt_publish_message(u8 *mqtt_message, char *topic, char *message, u8 qos);
u16 mqtt_onenetpublish_message(u8 *mqtt_message, char *topic, char *message, u16 len, u8 qos);
u16 mqtt_connect_message(u8 *mqtt_message, char *client_id, char *username, char *password);
u16 mqtt_subscribe_message(u8 *mqtt_message, char *topic, u8 qos, u8 whether);

void Send_HttpPkt(char *phead, char *pbody, char *dev_id);
void MQTTCONNECTServer(void);
void sendMQTTPkt(char *p, int len);
void Clear_Buffer(void);

#endif
